package com.liankuan.console.repository;

import com.liankuan.console.dto.StrategyExchange;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.util.List;

public interface StrategyExchangeRepository extends JpaRepository<StrategyExchange, Integer> {

    /**
     * 根据策略Id获取对应的交易所Id
     *
     * @param strategyId
     * @return
     */
    List<StrategyExchange> findAllByStrategyId(int strategyId);

    /**
     * 根据联合strategyId和exchangeId获取对应的记录
     *
     * @param strategyId
     * @param exchangeId
     * @return
     */
    StrategyExchange findAllByStrategyIdAndExchangeId(int strategyId, int exchangeId);

    /**
     * 根据联合strategyId和exchangeId进行跟新操作
     *
     * @param strategyId
     * @param exchangeId
     * @param pairs
     */
    @Query(value = "update strategy_exchange set pairs=?3 where strategy_id=?1 and exchange_id=?2", nativeQuery = true)
    @Modifying
    @Transactional
    void updateStrategyExchange(int strategyId, int exchangeId, String pairs);

    /**
     * 删除该策略对应的交易所信息
     *
     * @param strategyId
     * @param exchangeId
     */
    @Transactional
    void deleteByStrategyIdAndExchangeId(int strategyId, int exchangeId);

    /**
     * 删除该策略的所有交易所信息
     *
     * @param strategyId
     */
    void deleteAllByStrategyId(int strategyId);

    /**
     * 当删除某个交易所时，所有映射关系全部删除
     * @param exchangeId
     */
    void deleteAllByExchangeId(int exchangeId);
}
